package com.nextbusmn.server.services;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.google.appengine.labs.repackaged.org.json.JSONArray;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.nextbusmn.server.JDOWrappers.ShapeJDOWrapper;
import com.nextbusmn.server.JSONBuilders.ShapeJSONBuilder;
import com.nextbusmn.server.dataobjects.ShapeData;

public class MapService implements AndroidService {
	
	//private final String staticJSON = "[{\"y\":44989335,\"x\":-93251497},{\"y\":44989182,\"x\":-93251117},{\"y\":44989749,\"x\":-93250671},{\"y\":44989980,\"x\":-93250467},{\"y\":44990247,\"x\":-93249726},{\"y\":44990672,\"x\":-93248614},{\"y\":44990672,\"x\":-93248614},{\"y\":44990716,\"x\":-93248499},{\"y\":44989437,\"x\":-93245276},{\"y\":44989437,\"x\":-93245276},{\"y\":44989393,\"x\":-93245164},{\"y\":44988839,\"x\":-93243739},{\"y\":44988337,\"x\":-93242495},{\"y\":44988337,\"x\":-93242495},{\"y\":44988293,\"x\":-93242383},{\"y\":44987742,\"x\":-93240993},{\"y\":44987226,\"x\":-93239711},{\"y\":44987226,\"x\":-93239711},{\"y\":44987182,\"x\":-93239598},{\"y\":44987092,\"x\":-93239370},{\"y\":44986672,\"x\":-93238320},{\"y\":44986672,\"x\":-93238320},{\"y\":44986628,\"x\":-93238208},{\"y\":44985652,\"x\":-93238977},{\"y\":44984743,\"x\":-93239696},{\"y\":44984743,\"x\":-93239696},{\"y\":44984660,\"x\":-93239762},{\"y\":44983690,\"x\":-93240554},{\"y\":44982791,\"x\":-93241262},{\"y\":44982791,\"x\":-93241262},{\"y\":44982712,\"x\":-93241323},{\"y\":44981725,\"x\":-93242131},{\"y\":44981176,\"x\":-93240756},{\"y\":44980661,\"x\":-93239451},{\"y\":44980661,\"x\":-93239451},{\"y\":44980614,\"x\":-93239335},{\"y\":44980074,\"x\":-93237941},{\"y\":44979522,\"x\":-93236563},{\"y\":44979020,\"x\":-93235273},{\"y\":44979020,\"x\":-93235273},{\"y\":44978976,\"x\":-93235161},{\"y\":44978792,\"x\":-93235315},{\"y\":44978798,\"x\":-93235404},{\"y\":44978775,\"x\":-93235500},{\"y\":44978745,\"x\":-93235562},{\"y\":44978440,\"x\":-93235831},{\"y\":44978440,\"x\":-93235831},{\"y\":44978031,\"x\":-93236189},{\"y\":44977998,\"x\":-93236220},{\"y\":44977932,\"x\":-93236239},{\"y\":44977896,\"x\":-93236239},{\"y\":44977841,\"x\":-93236200},{\"y\":44977784,\"x\":-93236123},{\"y\":44977435,\"x\":-93236291},{\"y\":44976562,\"x\":-93236543},{\"y\":44976290,\"x\":-93236638},{\"y\":44975933,\"x\":-93236842},{\"y\":44975743,\"x\":-93236883},{\"y\":44974481,\"x\":-93236886},{\"y\":44973905,\"x\":-93236895},{\"y\":44973812,\"x\":-93236941},{\"y\":44973671,\"x\":-93237111},{\"y\":44973619,\"x\":-93237238},{\"y\":44973531,\"x\":-93237586},{\"y\":44972911,\"x\":-93241302},{\"y\":44972544,\"x\":-93243391},{\"y\":44972544,\"x\":-93243391},{\"y\":44972411,\"x\":-93244141},{\"y\":44972292,\"x\":-93245748},{\"y\":44972210,\"x\":-93247217},{\"y\":44971405,\"x\":-93247263},{\"y\":44971405,\"x\":-93247263},{\"y\":44971315,\"x\":-93247267},{\"y\":44970234,\"x\":-93247247},{\"y\":44968999,\"x\":-93247257},{\"y\":44968999,\"x\":-93247257},{\"y\":44967762,\"x\":-93247271},{\"y\":44967487,\"x\":-93247274},{\"y\":44967487,\"x\":-93247274},{\"y\":44966637,\"x\":-93247278},{\"y\":44966277,\"x\":-93247265},{\"y\":44965674,\"x\":-93247254},{\"y\":44965246,\"x\":-93247260},{\"y\":44965194,\"x\":-93246839},{\"y\":44965137,\"x\":-93246518},{\"y\":44964969,\"x\":-93245864},{\"y\":44964813,\"x\":-93245345},{\"y\":44964705,\"x\":-93244688},{\"y\":44964604,\"x\":-93244316},{\"y\":44964466,\"x\":-93243744},{\"y\":44964312,\"x\":-93242622},{\"y\":44964240,\"x\":-93241710},{\"y\":44964227,\"x\":-93240937},{\"y\":44964225,\"x\":-93240817},{\"y\":44964168,\"x\":-93240314},{\"y\":44964101,\"x\":-93239518},{\"y\":44964078,\"x\":-93238814},{\"y\":44964078,\"x\":-93237848},{\"y\":44963984,\"x\":-93236889},{\"y\":44963982,\"x\":-93236170},{\"y\":44964654,\"x\":-93236165},{\"y\":44965159,\"x\":-93236164},{\"y\":44965500,\"x\":-93236161},{\"y\":44965415,\"x\":-93235964},{\"y\":44965415,\"x\":-93235964},{\"y\":44965298,\"x\":-93235693},{\"y\":44964828,\"x\":-93234589},{\"y\":44964639,\"x\":-93234144},{\"y\":44964156,\"x\":-93233006},{\"y\":44963961,\"x\":-93232986},{\"y\":44962842,\"x\":-93233004},{\"y\":44962842,\"x\":-93233004},{\"y\":44962751,\"x\":-93233004},{\"y\":44962748,\"x\":-93234453},{\"y\":44962748,\"x\":-93234453},{\"y\":44962748,\"x\":-93234581},{\"y\":44962739,\"x\":-93236169},{\"y\":44962747,\"x\":-93237618},{\"y\":44962747,\"x\":-93237618},{\"y\":44962747,\"x\":-93237746},{\"y\":44962747,\"x\":-93239215},{\"y\":44962747,\"x\":-93239215},{\"y\":44962746,\"x\":-93239342},{\"y\":44962742,\"x\":-93240015},{\"y\":44962749,\"x\":-93240803},{\"y\":44962749,\"x\":-93240803},{\"y\":44962749,\"x\":-93240931},{\"y\":44962753,\"x\":-93242511},{\"y\":44962747,\"x\":-93244096},{\"y\":44962747,\"x\":-93244405},{\"y\":44962762,\"x\":-93244745},{\"y\":44962762,\"x\":-93244745},{\"y\":44962776,\"x\":-93244996},{\"y\":44962753,\"x\":-93245240},{\"y\":44962661,\"x\":-93246128},{\"y\":44962623,\"x\":-93246874},{\"y\":44962623,\"x\":-93246874},{\"y\":44962595,\"x\":-93247423},{\"y\":44962585,\"x\":-93247925},{\"y\":44962614,\"x\":-93248675},{\"y\":44962646,\"x\":-93248949},{\"y\":44962808,\"x\":-93250496},{\"y\":44962831,\"x\":-93250964},{\"y\":44962831,\"x\":-93250964},{\"y\":44962836,\"x\":-93251091},{\"y\":44962819,\"x\":-93251574},{\"y\":44962746,\"x\":-93252359},{\"y\":44962745,\"x\":-93252648},{\"y\":44962745,\"x\":-93252648},{\"y\":44962737,\"x\":-93253962},{\"y\":44962727,\"x\":-93254847},{\"y\":44962725,\"x\":-93255427},{\"y\":44962721,\"x\":-93256177},{\"y\":44962720,\"x\":-93256672},{\"y\":44962720,\"x\":-93256672},{\"y\":44962719,\"x\":-93256799},{\"y\":44962721,\"x\":-93257402},{\"y\":44962722,\"x\":-93258063},{\"y\":44962722,\"x\":-93258063},{\"y\":44962722,\"x\":-93258190},{\"y\":44962721,\"x\":-93259547},{\"y\":44962721,\"x\":-93259547},{\"y\":44962721,\"x\":-93259675},{\"y\":44962712,\"x\":-93261089},{\"y\":44962709,\"x\":-93262550},{\"y\":44962709,\"x\":-93262693},{\"y\":44962709,\"x\":-93262693},{\"y\":44962703,\"x\":-93263837},{\"y\":44962701,\"x\":-93264985},{\"y\":44962701,\"x\":-93264985},{\"y\":44962700,\"x\":-93265112},{\"y\":44962695,\"x\":-93266407},{\"y\":44962689,\"x\":-93267678},{\"y\":44962688,\"x\":-93267910},{\"y\":44962688,\"x\":-93267910},{\"y\":44962692,\"x\":-93268911},{\"y\":44962691,\"x\":-93270187},{\"y\":44962694,\"x\":-93271338},{\"y\":44962694,\"x\":-93271338},{\"y\":44962694,\"x\":-93271466},{\"y\":44962694,\"x\":-93272610},{\"y\":44962694,\"x\":-93272610},{\"y\":44962694,\"x\":-93272737},{\"y\":44962690,\"x\":-93274017},{\"y\":44962693,\"x\":-93275288},{\"y\":44962696,\"x\":-93276444},{\"y\":44962696,\"x\":-93276444},{\"y\":44962695,\"x\":-93276571},{\"y\":44962695,\"x\":-93277808},{\"y\":44962695,\"x\":-93277982},{\"y\":44962695,\"x\":-93277982},{\"y\":44962697,\"x\":-93279408},{\"y\":44962699,\"x\":-93280687},{\"y\":44962699,\"x\":-93280687},{\"y\":44962699,\"x\":-93280815},{\"y\":44962695,\"x\":-93281255},{\"y\":44962693,\"x\":-93282921},{\"y\":44962690,\"x\":-93283087},{\"y\":44962690,\"x\":-93283087},{\"y\":44962690,\"x\":-93283215},{\"y\":44962693,\"x\":-93284177},{\"y\":44962701,\"x\":-93285333},{\"y\":44962701,\"x\":-93285333},{\"y\":44962701,\"x\":-93285460},{\"y\":44962703,\"x\":-93286743},{\"y\":44962703,\"x\":-93288003},{\"y\":44962702,\"x\":-93288254},{\"y\":44962702,\"x\":-93288254},{\"y\":44962702,\"x\":-93289325},{\"y\":44962705,\"x\":-93290527},{\"y\":44962700,\"x\":-93291315},{\"y\":44962699,\"x\":-93291474},{\"y\":44962455,\"x\":-93291658},{\"y\":44962455,\"x\":-93291658},{\"y\":44962210,\"x\":-93291838},{\"y\":44962089,\"x\":-93291927},{\"y\":44961908,\"x\":-93291991},{\"y\":44961726,\"x\":-93292126}]";
	
	
	private final int shapeID;
	private HttpServletResponse response;
	
	public MapService(String request, HttpServletResponse response) {
		int tempID = 0;
		try {
			tempID = Integer.parseInt(request);
		} catch (NumberFormatException e) {
			AndroidService errorService = 
					new ErrorService("Number Format Exception parsing shapeID", response);
			errorService.processRequest();
		}
		shapeID = tempID;
		this.response = response;
	}
	
	@Override
	public void processRequest() {
		try {
			JSONArray jsonResponse = getShapeData();
			
			response.setContentType("application/json");
	        response.setHeader("Cache-Control", "no-cache");
	        response.getWriter().write(jsonResponse.toString());
	        
		} catch (IOException e) {
			AndroidService errorService = 
					new ErrorService("IOError on Map Service response", response);
			errorService.processRequest();
			// TODO create appropriate error
			//throw new AndroidRequestError(shapeID, "IOError on Map Service response");
		} 

	}
	
	private JSONArray getShapeData() {
		List<ShapeData> shapeData = 
				ShapeJDOWrapper.getShapesByID(shapeID);
		
		if (shapeData.size() == 0) {
			AndroidService errorService =
					new ErrorService("Shape ID returned no results", response);
			errorService.processRequest();
			// TODO create appropriate error
			//throw new AndroidRequestError(shapeID, "Shape ID returned no results");
		}
		
		return ShapeJSONBuilder.getShapeJSON(shapeData);
	}

}
